<template>
	<view class="search_continue">
		<!-- 搜索框 -->
		<view class="search-item">
		 <uni-search-bar :focus="true"  :radius="18" @input="input" cancel-button="none"  ></uni-search-bar>
		</view>
		<!-- 搜索建议列表 -->
		<view class="suggest" v-if="kw.length !== 0">
			<view class="suggest-item" v-for="(item,i) in searchList" :key="i" @click="gotoGoods(item)">
				<view>{{item.goods_name}}</view>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<!-- 搜索历史展示 -->
		<view class="history" v-else>
			<view class="history-title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="17" @click="clean"></uni-icons>
			</view>
			<view class="histor-list">
				<uni-tag :text="item" v-for="(item,i) in history" :key="i" @click="gotoGoods(item)"></uni-tag>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			   // 延时器
		    	timer:null,
			    // 搜索关键字
			    kw:'',
				// 搜索建议列表数据
				searchList:[],
				//搜索历史展示
				historyList:['手机','电脑']
			};
		},
		onLoad() {
          // this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
			},
		methods: {
			input(e){
				clearTimeout(this.timer)
				this.timer = setTimeout(() =>{
					this.kw = e
					this.getSearchList()
					// console.log(this.kw)
					this.saveHistoryList()
				},500)
			},
			// 获取搜索建议列表数据
			async getSearchList() {
				if(this.kw.length === ''){
					this.searchList = []
					return
				}
				const {data:res} = await uni.$http.get('public/v1/goods/qsearch',{query:this.kw})
				// console.log(res)
				this.searchList = res.message
			},
			// 点击跳转至搜索详情
			gotoGoods(item) {
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			// 存储搜索关键字，push追加
			saveHistoryList() {
				// this.historyList.push(this.kw)
				const set = new Set(this.historyList)
				set.delete(this.kw)
				set.add(this.kw)
				this.historyList = Array.from(set)
		        // 持久化存储到本地
				uni.setStorageSync('kw',JSON.stringify(this.historyList))
			},
			// 点击删除清空搜索历史
			clean() {
				this.historyList = []
				uni.setStorageSync('kw','')
			},
			// 点击搜索历史跳转至商品列表
			gotoGoods(kw) {
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?query=' + kw
				})
			}
		},
		computed:{
			// 解决搜索关键字排列问题
			history(){
				return [...this.historyList].reverse()
			}
		}
	}
</script>

<style lang="scss">
	// 搜索建议样式
 .suggest{
	 width: 100%;
    .suggest-item{
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		border-bottom: 1px solid #efefef;
		margin-top: 20px;
		view{
			font-size: 14px;
			// 不允许换行
			white-space: nowrap;
			// 溢出隐藏
			overflow: hidden;
			// 溢出隐藏部分用省略号表示
			text-overflow: ellipsis;
			margin-right: 3px;
		}
	}
 }
 // 搜索历史样式
 .history{
	 display: flex;
	 flex-direction: column;
	 .history-title{
		 display: flex;
		 justify-content: space-between;
		 margin: 5px 0;
		 border-bottom: 1px solid #efefef;
		 text,.uni-icons{
			 font-size: 16px;
			 margin: 5px;
		 }
	 }
 }
 .histor-list{
	 display: flex;
	 flex-wrap: wrap;
 }
 .uni-tag{
	 margin: 5px;
 }
</style>
